package com.pig4cloud.pigx.common.security.resource.component.beanDefinitionRegistrar;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.web.AuthenticationEntryPoint;

/**
 * @description: 支持本地模式 [不经过pigx-auth中的CheckToken]资源服务器配置
 * @Author 兔子不吃窝边曹
 * @create 2022/8/5 15:48
 **/
@Slf4j
public class PigxLocalResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {

    @Autowired
    private ResourceServerTokenServices resourceServerTokenServices;

    @Autowired
    protected AuthenticationEntryPoint resourceAuthExceptionEntryPoint;

    /**
     * @param httpSecurity
     */
    @Override
    @SneakyThrows
    public void configure(HttpSecurity httpSecurity) {
        // 允许使用iframe 嵌套，避免swagger-ui 不被加载的问题
        httpSecurity.headers().frameOptions().disable();

        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity
                .authorizeRequests();
        // TODO 配置对外暴露接口
        // ....


        registry.anyRequest().authenticated();

        httpSecurity.csrf().disable();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources
                .tokenServices(resourceServerTokenServices)
                .authenticationEntryPoint(resourceAuthExceptionEntryPoint)
        ;
    }

}

